package com.xiaoyung.easyisapisdk.common.util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class CamelFieldNameUtils {
	/**
	 * 驼峰转下划线
	 */
	public static void camelToUnderlineKey(Object json) {//map jsonObject jsonArr
		underlineKeyToCamel(json,false);
	}

	/**
	 * 下划线转驼峰
	 */
	public static void underlineKeyToCamel(Object json) {//map jsonObject jsonArr
		underlineKeyToCamel(json,true);
	}

	
	
	private static void underlineKeyToCamel(Object json,boolean toCamel) {
		if(json instanceof Map) {
			underlineKeyToCamel((Map)json,toCamel);
		}else if(json instanceof List) {
			underlineKeyToCamel((List)json,toCamel);
		}
	}
	/**
	 * 下划线key 替换成 驼峰 数组类型 
	 */
	private static void underlineKeyToCamel(List arr,boolean toCamel) {
		if(arr==null || arr.isEmpty()) {
			return;
		}
		for (int i = 0; i < arr.size(); i++) {
			Object vObj = arr.get(i);
			if(vObj instanceof Map) {
				underlineKeyToCamel((Map)vObj,toCamel);
			}else if(vObj instanceof List) {
				underlineKeyToCamel((List)vObj,toCamel);
			}
		}
	}
	
	/**
	 * 下划线key 替换成 驼峰 
	 */
	private static void replaceUnderlineKeyToCamelKey(Map obj,String key,Object valueObj,boolean toCamel) {
		//有些特殊字段_orderId  __xxx,因此  _开头的不要转换
		String prefix = null;
		String undelineKey = key;
		if(key.startsWith("__")) {//好像没有这种
			//if(key.startsWith("_"));
			undelineKey = key.substring(2);
			prefix = "__";
		}else if(key.startsWith("_")) {
			undelineKey = key.substring(1);
			prefix = "_";
		}
		
		if((toCamel && undelineKey.indexOf("_")>-1) || (!toCamel) ) {
			String calmeKey = toCamel?StringUtils.underlineToCamel(undelineKey):StringUtils.camelToUnderline((undelineKey));
			if(prefix!=null) {
				calmeKey= prefix+calmeKey;
			} 
			obj.put(calmeKey, valueObj);
			obj.remove(key);//删除下划线key
		}
		
	}
	/**
	 * 下划线转驼峰 
	 */
	private static void underlineKeyToCamel(Map<String, Object> obj,boolean toCamel) {
		if(obj!=null) {
			Set<String> keys = obj.keySet();
			String[] arr = keys.toArray(new String[keys.size()]);//keys转成数组 不然会并发修改map异常, 因为 map中的key又添加又删除
			System.out.println("keys "+ Arrays.asList(arr));
			for(String key : arr) {
				Object valueObj = obj.get(key);
				if(valueObj instanceof Map) {
					underlineKeyToCamel((Map<String, Object>)valueObj,toCamel);
				}else if(valueObj instanceof JSONArray) {
					underlineKeyToCamel((List)valueObj,toCamel);
				}else {
					//简单值属性
					replaceUnderlineKeyToCamelKey(obj,key,valueObj,toCamel);
				}
			}
		}
	}
	
	public static void main(String[] args) {
		JSONObject obj = JSON.parseObject("{__aa_bb_cc:2232,_dict_value_1:11,name_na:1,field:1,list:[{dd_dd:1112,obj:{name_e:333,age_d:22}}] }");
		//JSONObject obj = JSON.parseObject("{__aa_bb_cc:2232,_dict_value_1:11,name_na:1,field:1,list:[{dd_dd:1112,obj:{name_e:333,age_d:22}}]}");
		underlineKeyToCamel(obj,true);
		System.out.println("转驼峰:");
		System.out.println(obj);
		underlineKeyToCamel(obj,false);
		System.out.println("转下划线:");
		System.out.println(obj);
	}
}
